<?php
/**
 * 轿辰会通知接口类
 * Created by www.soe-soe.com
 * Author: shijy
 * Date: 2016/7/7
 * Time: 11:18
 */

namespace Common\Model\OtherSystem;

use Common\Model\UtilsModel;

class knb extends NoticeOtherSystem
{
    private $key='123';
    private $noticeUrl='http://223.corp.soe-soe.com/sjy_server/api.php?c=jch&';


    /**
     * cli方式请求数据
     * Author: shijy
     * @param $actionName
     * @param $param
     * @return bool|mixed
     */
    public function notice_cli($actionName,$param)
    {
        return false;

        ksort($param);
        $paramStr=http_build_query($param);
        $now=time();

        $sign=$this->mkSign($now,$paramStr);
        $paramStr .="&T={$now}&S={$sign}";
        $paramStr=urlencode($paramStr);
        exec("/usr/bin/php  ".WEB_ROOT."/cliApi.php Api/Jch/{$actionName} {$paramStr}",$output,$status);
        if($status!=0){
            $this->addNoticeOtherSystemLog(COMPANY_JCH,$actionName,$param,0,NoticeOtherSystem::STATUS_FAILURE_SYSTEM);
            return false;
        }
        $resultStr  =implode($output);
        $resultObj  =json_decode($resultStr,true);
        if(!$resultObj || $resultObj['status']!=0){
                $this->addNoticeOtherSystemLog(COMPANY_JCH,$actionName,$param,$resultStr,NoticeOtherSystem::STATUS_FAILURE_FEEDBACK);
                return false;
        }
        $this->addNoticeOtherSystemLog(COMPANY_JCH,$actionName,$param,$resultStr,NoticeOtherSystem::STATUS_SUCCESS);
        return $resultObj;
    }


    /**
     * cgi方式请求数据
     * Author: shijy
     * @param $actionName
     * @param $param
     * @return bool|mixed
     */
    public function notice_cgi($actionName,$param=null)
    {
      
        $now=time();
        $sign=$this->mkSign($actionName,$now,$param);
        $url=$this->noticeUrl.'a='.$actionName.'&sg='.$sign.'&t='.$now;
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        if(is_null($param)){
            curl_setopt($ch, CURLOPT_HEADER, 0);
        }else{
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
        }
        $data = curl_exec($ch);
        $data=json_decode($data,true);





        if(is_null($data)){
            $this->addNoticeOtherSystemLog(COMPANY_JCH,$actionName,$param,0,NoticeOtherSystem::STATUS_FAILURE_SYSTEM);
            return false;
        }
        $resultStr  =implode($output);
        $resultObj  =json_decode($resultStr,true);
        if(!$resultObj || $resultObj['status']!=0){
            $this->addNoticeOtherSystemLog(COMPANY_JCH,$actionName,$param,$resultStr,NoticeOtherSystem::STATUS_FAILURE_FEEDBACK);
            return false;
        }
        $this->addNoticeOtherSystemLog(COMPANY_JCH,$actionName,$param,$resultStr,NoticeOtherSystem::STATUS_SUCCESS);
        return $resultObj;
    }

    /**
     * sign算法
     * Author: shijy
     * @param $actionName 行为名称
     * @param $timestamp 时间戳
     * @param $param  将被post的数据
     * @return string sign签名
     */
    public function mkSign($actionName,$timestamp,$param=null){
        $temp=array();
        $temp['sk']    = $this->key;
        $temp['act']   = $actionName;
        $temp['t']     = $timestamp;
        if(!is_null($param)){
            $temp['data']  =md5(json_encode($param));
        }
        ksort($temp);
        $paramStr=http_build_query($temp);
         return md5($paramStr);
    }
    
}